--[[
	=========================
====	Display Functions
	=========================
]]--
function adBottomArtSetWidth(v)
	for i = 1, 8 do
		getglobal("adBottomArtFrameBG"..i):SetWidth(v);
		getglobal("adBottomArtFrameAWT"..i):SetWidth(v);
		getglobal("adBottomArtFrameOV"..i):SetWidth(v);
	end
end

function adBottomArtSetHeight(v)
	for i = 1, 8 do
		getglobal("adBottomArtFrameBG"..i):SetHeight(v);
		getglobal("adBottomArtFrameAWT"..i):SetHeight(v);
		getglobal("adBottomArtFrameOV"..i):SetHeight(v);
	end
end

function adBottomArtSetSkin(v)
	for i = 1, 8 do
		getglobal("adBottomArtFrameBG"..i):SetTexture("Interface\\AddOns\\XBar\\Textures\\"..v.."\\bg"..i..".tga");
		getglobal("adBottomArtFrameAWT"..i):SetTexture("Interface\\AddOns\\XBar\\Textures\\"..v.."\\awt"..i..".tga");
		getglobal("adBottomArtFrameOV"..i):SetTexture("Interface\\AddOns\\XBar\\Textures\\"..v.."\\ov"..i..".tga");
	end
end

function adBottomArtSetColor(v)
end
--[[
	=============================
====	Data Saving Functions
	=============================
]]--
function adBottomArt_LoadSettings()
	SaveVariables("adSettings");
	local adDefaultSettings = {
			["ArtScale"] = 1,
			["ArtAlpha"] = 1,
			["ArtWidth"] = 512,
			["ArtHeight"] = 256,
			["ArtName"] = "blackbar",
			["ArtShown"] = false
		};
	if (not adSettings) then adSettings = {}; end
    if (not adSettings["adBottomArt"]) then
		adSettings["adBottomArt"] = adDefaultSettings;
	else
		for k,v in pairs(adDefaultSettings) do
			if (adSettings["adBottomArt"][k] == nil) then
				adSettings["adBottomArt"][k] = v;
			end
		end
	end
end

--[[
	==============
====	EVENTS
	==============
]]--
function adBottomArt_OnEvent(event, this)
	if (event == "VARIABLES_LOADED" ) then
		adBottomArt_LoadSettings();
		if adSettings["adBottomArt"]["ArtScale"] ~= nil then adBottomArtFrame:SetScale(adSettings["adBottomArt"]["ArtScale"]); end
		if adSettings["adBottomArt"]["ArtAlpha"] ~= nil then adBottomArtFrame:SetAlpha(adSettings["adBottomArt"]["ArtAlpha"]); end
		if adSettings["adBottomArt"]["ArtWidth"] ~= nil then adBottomArtSetWidth(adSettings["adBottomArt"]["ArtWidth"]); end
		if adSettings["adBottomArt"]["ArtHeight"] ~= nil then adBottomArtSetHeight(adSettings["adBottomArt"]["ArtHeight"]); end
		if adSettings["adBottomArt"]["ArtName"] ~= nil then adBottomArtSetSkin(adSettings["adBottomArt"]["ArtName"]); end
		if adSettings["adBottomArt"]["ArtShown"] ~= nil and adSettings["adBottomArt"]["ArtShown"] == false then adBottomArtFrame:Hide(); end
	end
end

--[[
	======================
====	SLASH COMMANDS
	======================
]]--
function adBottomArt_GetWords(str)
	local ret = {};
	local pos=0;
	while(true) do
		local word;
		_,pos,word=string.find(str, "^ *([^%s]+) *", pos+1);
		if(not word) then
			return ret;
		end
		table.insert(ret, word);
	end
end
function adBottomArtSlash(ebox, v)
	local v = string.lower(v);
	local wL = adBottomArt_GetWords(v);
	
	if wL[1] == "alpha" then
		adSettings["adBottomArt"]["ArtAlpha"] = wL[2];
		adBottomArtFrame:SetAlpha(wL[2]);
	elseif wL[1] == "scale" then
		adSettings["adBottomArt"]["ArtScale"] = wL[2];
		adBottomArtFrame:SetScale(wL[2]);
	elseif wL[1] == "width" then
		adSettings["adBottomArt"]["ArtWidth"] = wL[2];
		adBottomArtSetWidth(wL[2]);
	elseif wL[1] == "height" then
		adSettings["adBottomArt"]["ArtHeight"] = wL[2];
		adBottomArtSetHeight(wL[2]);
	elseif wL[1] == "show" then
		adSettings["adBottomArt"]["ArtShown"] = true;
		adBottomArtFrame:Show();
	elseif wL[1] == "hide" then
		adSettings["adBottomArt"]["ArtShown"] = false;
		adBottomArtFrame:Hide();
	elseif wL[1] == "skin" then
		adSettings["adBottomArt"]["ArtName"] = wL[2];
		adBottomArtSetSkin(wL[2]);
	elseif wL[1] == "autoscale" then
		if wL[2] == "width" then
			adBottomArtSlash(nil, "width " .. tostring(512 * GetScreenWidth() / 2048))
		else
			adBottomArtSlash(nil, "scale " .. tostring(GetScreenWidth() / 2048))
		end
	else
		ShowUIPanel(adBottomArtConfigFrame);
	end
	
	SaveVariables("adSettings");
end

SLASH_adBottomArtSlash1 = "/adBottomArt";
SLASH_adBottomArtSlash2 = "/adBA";
SlashCmdList["adBottomArtSlash"] = adBottomArtSlash;
